<?php

/**
 * Description of Packager
 *
 * @author user
 */
class Packager {
    public static function diff($text1, $text2) {
        $result = array();
        $p1 = 0;
        $p2 = 0;
        // делим переданные строки на части
//        if (strpos($text1, "\n") !== false) {
//            $ex = "\n";
//            echo "EX: '\\n'<br/>";
//        } else {
            $chrs = count_chars($text1);
            arsort($chrs);
            $ex = array_shift(array_keys($chrs));
            $ex = chr($ex);
            echo "EX: '$ex'\n";
//        }
        $a1 = explode($ex, $text1);
        $a2 = explode($ex, $text2);
        $eq = true;
        $p1 = 0;
        $p2 = 0;
        while($eq && ($p1 < count($a1)) && ($p2 < count($a2))) {
            if ($a1[$p1] == $a2[$p2]) {
            } else {
                $o1 = $p1;
                $o2 = $p2;
                if (self::find_eq($a1, $a2, $p1, $p2, 0, true)) {
                    if ($p1 < $p2) {
                        echo "+\t$o1\t";
                        for($x=$o2; $x< $p2; $x++) {
                            echo $a2[$x].$ex;
                        }
                        echo "\n";
                    } else {
                        echo "-\t$o1\t";
                        for($x=$o1; $x< $p1; $x++) {
                            echo $a1[$x].$ex;
                        }
                        echo "\n";
                    }
                } else {
                        echo "%\t$p1\t$a1[$p1].$ex...$a2[$p2].$ex\n";
                }
            }
            $p1++;
            $p2++;
        }
    }

    private static function mk_response($a1, $a2, $diff, $ex) {
        echo "<h2>Diff:</h2><br/><pre>".print_r($diff, true).'<hr/>';
        $k1 = array_keys($diff[0]);
        $k2 = array_keys($diff[1]);

        $edited = array_intersect($k1, $k2);
        $removed = array_diff($k1, $k2);
        $added = array_diff($k2, $k1);
        echo "<h2>edited:</h2><br/><pre>".print_r($edited, true).'<hr/>';
        echo "<h2>removed:</h2><br/><pre>".print_r($removed, true).'<hr/>';
        echo "<h2>added:</h2><br/><pre>".print_r($added, true).'<hr/>';

    }

    private static function chunk_pos($a, $id, $ex) {
        $before = array_splice($a, $id);
        $str = implode($ex, $a);
        return strlen($str);
    }

    private static function find_eq($a1, $a2, &$p1, &$p2, $br, $start = NULL) {
//        echo "branch $br <$p1, $p2>(".count($a1).','.count($a2).") started<br/>";
        if (($p1 > count($a1)) or ($p2 > count($a2))) {
//            echo "floor reached.<br/>";
            return false;
        }
        if ($br == 0) {
//            echo "zero branch starting...<br/>";
            if (($a1[$p1+1] == $a2[$p2]) && ($a1[$p1+2] == $a2[$p2+1])) {
                $p1++;
//                echo "eq found at <$p1, $p2><br/>";
                return true;
            } else {
                $np = $p1+1;
//                echo "<blockquote>";
                if (self::find_eq($a1, $a1, $np, $p2, $br)) {
//                    echo "</blockquote>";
                    $p1 = $np;
                    return  true;
                }
//                echo "</blockquote>";
            }
        }

        if ($p1-$p2 == 1) return false;
//        echo "br++<br/>";
        $br++;
        if (($a1[$p1] == $a2[$p2+1]) && ($a1[$p1+1] == $a2[$p2+2])) {
            $p2++;
//            echo "eq found at <$p1, $p2><br/>";
            return true;
        } else {
            $np = $p2+1;
//            echo "<blockquote>";
            if (self::find_eq($a1, $b1, $p1, $np, $br)) {
//                echo "</blockquote>";
                $p2 = $np;
                return  true;
            }
//            echo "</blockquote>";
        }
        return false;
    }

    function revert()
}

/**
 * Формат элемента массива ответа diff:
 *      id  значение
 *      0   операция: +(добавление фрагмента) -(удаление фрагмента) %(замена фрагмента)
 *      1   левое значение
 *      2   правое значение
 */
?>
